<template>
  <div>
    <el-container>
      <el-main>
        <div style="margin-bottom:15px;text-align: left">
          <el-input style="width: 240px" placeholder="捐献人" size="small" v-model="params.donors"></el-input>
          <el-input style="width: 240px; margin-left: 15px" placeholder="血型" size="small" v-model="params.bloodType"></el-input>
          <el-button style="margin-left: 15px" size="small" type="primary" @click="getall"><i class="el-icon-search"></i> 搜索</el-button>
          <el-button style="margin-left: 15px" size="small" type="warning" @click="reset"><i class="el-icon-refresh"></i> 重置</el-button>
        </div>
        <div style="text-align: left" >
          <el-button style="margin-left: 0px" size="small" type="primary" @click="addinfo"><i class="el-icon-circle-plus-outline"></i>     添加信息</el-button>
        </div>
        <el-table
            :border="true"
            :data="tableData"
            stripe
            style="width: 100%;margin-top: 20px"
        >
          <el-table-column
              prop="id"
              label="编号"
              width="100">
          </el-table-column>
          <el-table-column
              prop="donors"
              label="捐献人"
              width="120">
          </el-table-column>
          <el-table-column
              prop="donoNum"
              label="献血量(支持筛选)"
              width="180"
              :filters="[{ text: '200cc', value: '200' }, { text: '300cc', value: '300' }, { text: '400cc', value: '400' }]"
              :filter-method="filterTag"
              filter-placement="bottom-end">
            <template slot-scope="scope">
              <el-tag
                  :type="scope.row.donoNum === '200' ? 'primary' : 'success'"
                  disable-transitions>{{scope.row.donoNum === '200' ? "200 cc":scope.row.donoNum === '300' ? "300 cc":"400 cc" }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
              prop="bloodType"
              label="血型(支持筛选)"
              width="135"
              :filters="[{ text: 'A型', value: 'A' }, { text: 'B型', value: 'B' }, { text: 'O型', value: 'O' }, { text: 'AB型', value: 'AB' }]"
              :filter-method="filterHandler"
              filter-placement="bottom-end">
            <template slot-scope="scope">
              {{scope.row.bloodType}} 型
            </template>
          </el-table-column>
          <el-table-column
              prop="donoTime"
              label="捐献时间"
              width="210">
          </el-table-column>
          <el-table-column
              prop="updateTime"
              label="更新时间"
              width="210">
          </el-table-column>
          <el-table-column
              prop="optioner"
              label="操作人"
              width="120">
          </el-table-column>
          <el-table-column label="操作"  width="200">
            <template #default="scope">
<!--                        scope.row 就是当前行数据-->
              <el-button type="primary" size="small" @click="handleEdit(scope.row)"><i class="el-icon-edit-outline"></i>  编 辑</el-button>
              <el-popconfirm
                  style="margin-left: 5px"
                  title="您确定删除这行数据吗？"
                  @confirm="del(scope.row.id)"
              >
                <el-button type="danger" size="small" slot="reference"><i class="el-icon-delete"></i>  删 除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!--    分页-->
        <div style="margin-top: 20px">
          <el-pagination
              background
              :current-page="params.pageNum"
              :page-size="params.pageSize"
              layout="prev, pager, next, jumper"
              @current-change="handleCurrentChange"
              :total="total">
          </el-pagination>
        </div>
        <!--      修改-->
        <div style="margin: 0px 0">
          <el-dialog
              :visible.sync="centerDialogVisibleEdit"
              title="修改"
              width="30%"
              center>
            <el-form :model="form" ref="ruleForm" :rules="rules" label-width="80px">
                  <el-form-item label="捐献人">
                    <el-input v-model="form.donors" style="width: 90%" disabled/>
                  </el-form-item>
              <el-row :gutter="20">
                <el-col :span="10">
                  <el-form-item label="血   型">
                    <el-select v-model="form.bloodType" style="width: 100%" placeholder="请选择">
                      <el-option
                          v-for="item in bloodTypes"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="14">
                  <el-form-item label="献血量">
                    <el-select v-model="form.donoNum" style="width: 80%" placeholder="请选择">
                      <el-option
                          v-for="item in donoNums"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        <span style="float: left">{{ item.label }}</span>
                        <!--                        <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>-->
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="捐献时间">
                <el-input v-model="form.donoTime" style="width: 90%;" disabled/>
              </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                  <el-button @click="centerDialogVisibleEdit = false">取消</el-button>
                  <el-button type="primary" @click="save">确认</el-button>
                </span>
            </template>
          </el-dialog>
        </div>

        <!--      添加-->
        <div style="margin: 0px 0">
          <el-dialog
              :visible.sync="centerDialogVisibleAdd"
              title="添加"
              width="30%"
              center>
            <el-form :model="addform" ref="ruleForm" :rules="rules" label-width="80px">
              <el-row :gutter="20">
                <el-col :span="10">
                  <el-form-item label="捐献人">
                    <el-input v-model="addform.donors" style="width: 100%" placeholder="捐献人"/>
                  </el-form-item>
                </el-col>
                <el-col :span="14">
                  <el-form-item label="联系方式">
                    <el-input v-model="addform.phone" style="width: 80%" placeholder="联系方式"/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="血液来源">
                <el-select v-model="addform.origin" style="width: 90%" placeholder="请选择">
                  <el-option
                      v-for="item in origins"
                      :value="item">
                    {{ item }}
                  </el-option>
                </el-select>
              </el-form-item>
              <el-row :gutter="20">
                <el-col :span="10">
                  <el-form-item label="年龄">
                    <el-input v-model="addform.age" style="width: 100%" placeholder="年龄"/>
                  </el-form-item>
                </el-col>
                <el-col :span="14">
                  <el-form-item label="性别">
                    <el-select v-model="addform.sex" style="width: 80%" placeholder="请选择">
                      <el-option
                          v-for="item in sexes"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        <span style="float: left">{{ item.label }}</span>
                        <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="10">
                  <el-form-item label="血   型">
                    <el-select v-model="addform.bloodType" style="width: 100%" placeholder="请选择">
                      <el-option
                          v-for="item in bloodTypes"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="14">
                  <el-form-item label="献血量">
                    <el-select v-model="addform.donoNum" style="width: 80%" placeholder="请选择">
                      <el-option
                          v-for="item in donoNums"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        <span style="float: left">{{ item.label }}</span>
                        <!--                        <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>-->
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="捐献时间">
                <el-date-picker
                    style="width: 91%"
                    v-model="addform.donoTime"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择日期时间">
                </el-date-picker>
              </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                  <el-button @click="centerDialogVisibleAdd = false">取消</el-button>
                  <el-button type="primary" @click="confirmadd">确认</el-button>
                </span>
            </template>
          </el-dialog>
        </div>
      </el-main>
    </el-container>
  </div>
</template>
<script>
import request from "@/utils/request";

export default {
  data(){
    return {
      tableData: [],
      total:0,
      origins:[], //下拉菜单选择信息
      params: {
        pageNum: 1,
        pageSize: 7,
        donors: '',
        bloodType: ''
      },
      sexes:[{
        value: '男',
        label: 'Man'
      }, {
        value: '女',
        label: 'Woman'
      }],
      donoNums:[{
        value: '200',
        label: '200 cc'
      }, {
        value: '300',
        label: '300 cc'
      }, {
        value: '400',
        label: '400 cc'
      }],
      bloodTypes:[{
        value: 'A',
        label: 'A型'
      }, {
        value: 'B',
        label: 'B型'
      },{
        value: 'O',
        label: 'O型'
      }, {
        value: 'AB',
        label: 'AB型'
      }],
      form:{
        donors: '',
        donoNum:'',
        bloodType:'',
        donoTime:'',
        optioner:''
      },
      addform:{
        donors: '',
        sex:'',
        age:'',
        phone:'',
        donoNum:'',
        origin:'',
        bloodType:'',
        donoTime:'',
        optioner:''
      },
      rules: {
        bloodType: [{required: true, message: '请输入来源', trigger: 'blur'}],
        donoNum: [{required: true, message: '请输入状态', trigger: 'blur'}],
      },
      centerDialogVisibleEdit:false, //编辑弹窗
      centerDialogVisibleAdd:false, //添加弹窗
    }
  },
  created() {
    this.getall()
    this.getOptions()
  },
  methods:{
    getall(){
      request.get('history/page',{
        params: this.params
      }).then(res=>{
        if(res.code === '200'){
          this.tableData = res.data.list
          this.total=res.data.total
        }
      })
    },
    async getOptions() {
      request.get('sites/sitesandcars',{

      }).then(res=>{
        if(res.code === '200'){
          this.origins = res.data;
        }
      })
    },
    reset(){
      this.params = {
        pageNum: 1,
        pageSize: 7,
        donors: '',
        bloodType: ''
      }
      this.getall()
    },
    del(id){
      request.delete('history/deleteOne/'+id).then(res=>{
        if (res.code === '200') {
          this.$notify.success('删除成功')
          this.getall()
        } else {
          this.$notify.error(res.msg)
        }
      })
    },
    confirmadd(){
      this.addform.optioner=JSON.parse(localStorage.getItem('name')).name
      request.post('history/addOne',this.addform).then(res=> {
        if (res.code === '200') {
          this.$message({
            type: "success",
            message: "添加信息成功！"
          })
          this.addform={}
          this.centerDialogVisibleAdd = false
          this.getall()
        } else {
          this.$message({
            type: "warning",
            message: res.msg
          })
          this.addform={}
          this.centerDialogVisibleAdd = false
          this.getall()
        }
      })
    },
    async save(){
      this.form.optioner=JSON.parse(localStorage.getItem('name')).name
      if(this.form.id){
        this.$refs['ruleForm'].validate((valid) => {
          if(valid){
            request.put('history/updateOne',this.form).then(res=>{
              if(res.code === '200'){
                this.$message({
                  type:"success",
                  message:"更新信息成功！"
                })
                this.centerDialogVisibleEdit=false
                this.getall()
              }else{
                this.$message({
                  type:"warning",
                  message: res.msg
                })
              }
            })
          }
        })
      }
    },
    handleCurrentChange(pageNum) {
      // 点击分页按钮触发分页
      this.params.pageNum = pageNum
      this.getall()
    },
    handleEdit(row){
      this.form=JSON.parse(JSON.stringify(row))
      this.centerDialogVisibleEdit=true
    },
    addinfo(){
      this.centerDialogVisibleAdd=true
    },
    filterTag(value, row) {
      return row.donoNum === value;
    },
    filterHandler(value, row, column) {
      const property = column['property'];
      return row[property] === value;
    }
  }
}
</script>
<style>

</style>